package com.woniuxy.joylifemarket.controller;

import com.alibaba.fastjson.JSON;
import com.woniuxy.common.entity.MarketAppraise;
import com.woniuxy.joylifemarket.service.MarketAppraiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class AppraiseController {
    @Autowired
    MarketAppraiseService marketAppraiseService;

    //搜索用户收到所有评价
    @RequestMapping("/inituserappraise")
    public Object userAppraise(String userid) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<MarketAppraise> allAppraise = marketAppraiseService.findAllAppraiseByUserId(userid);
            result.put("code", 200);
            result.put("msg", "查询成功");
            result.put("data", allAppraise);
//            System.out.println("allAppraise"+allAppraise);
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "查询失败");
            e.printStackTrace();
            return result;
        }
    }

    //发送评价
    @RequestMapping("/putappraise")
    public Object putAppraise(String appraise,String orderId) {
        Map<String, Object> result = new HashMap<>();
        MarketAppraise marketAppraise = JSON.parseObject(appraise, MarketAppraise.class);
        System.out.println("marketAppraise"+marketAppraise);
        try {
            int i = marketAppraiseService.insertAppraise(marketAppraise,orderId);
            if (i!=0) {
                result.put("code", 200);
                result.put("msg", "查询成功");
                return result;
            }
            result.put("code", 500);
            result.put("msg", "查询失败");
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "查询失败");
            e.printStackTrace();
            return result;
        }
    }
}
